/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "Px碰撞几何体.h"


using namespace physx;





void S_Px平面几何体::f_set几何体物理数据(void* data) {
	m_plane.n.x = ((vec4*)data)->x;
	m_plane.n.y = ((vec4*)data)->y;
	m_plane.n.z = ((vec4*)data)->z;
	m_plane.d = -(((vec4*)data)->w*2);
	return;
}

S_Px平面几何体::S_Px平面几何体(const vec4& v) {
	m_物理引擎枚举 = E_物理引擎::E_物理引擎_PhysX;
	m_Type = E_物理碰撞几何类型::E_碰撞几何_平面;
	

	m_plane = PxPlane({ v.x, v.y, v.z }, -v.w);
}

/*void S_Px凸壳几何体::f_set几何体物理数据(void* data) {
	S_三角Mesh& mesh = *((S_三角Mesh*)data);

	physx::PxTriangleMeshDesc 网格描述;

	网格描述.points.count = mesh.m_vNum;
	网格描述.points.stride = sizeof(vec3);
	网格描述.points.data = mesh.m_vert;

	网格描述.triangles.count = mesh.m_iNum;
	网格描述.triangles.stride = sizeof(uvec3);
	网格描述.triangles.data = mesh.m_index;
	//网格描述.flags;

	//for (uint32 i = 0; i < mesh.m_vNum; ++i) cout << mesh.m_vert[i].z << endl;
	//for (uint32 i = 0; i < mesh.m_iNum; ++i) cout << mesh.m_index[i] << endl;
	physx::PxDefaultMemoryOutputStream buf;
	physx::PxTriangleMeshCookingResult::Enum result;


	//auto& ic = phy->getPhysicsInsertionCallback();
	bool status = Cooking->cookTriangleMesh(网格描述, buf, &result);
	//physx::PxTriangleMesh* m = Cooking->createTriangleMesh(网格描述, ic, &result);
	if (!status)
		return NULL;


	physx::PxDefaultMemoryInputData readBuffer(buf.getData(), buf.getSize());
	return phy->createTriangleMesh(readBuffer);

}*/




S_Px方体几何体::S_Px方体几何体(const vec3& v) {
	m_物理引擎枚举 = E_物理引擎::E_物理引擎_PhysX;
	m_Type = E_物理碰撞几何类型::E_碰撞几何_方体;
	m_Gem = PxBoxGeometry(v.x, v.y, v.z);
	
}








S_Px凸壳几何体::S_Px凸壳几何体(S_Px物理引擎核心* core, const S_三角Mesh mesh, const vec3& scale) {
	m_物理引擎枚举 = E_物理引擎::E_物理引擎_PhysX;
	m_Type = E_物理碰撞几何类型::E_碰撞几何_凸壳;

	physx::PxConvexMeshDesc 网格描述;
	网格描述.points.count = mesh.m_vNum;
	网格描述.points.stride = sizeof(vec3);
	网格描述.points.data = mesh.m_vert;
	网格描述.flags = physx::PxConvexFlag::eCOMPUTE_CONVEX;
	网格描述.flags |= physx::PxConvexFlag::eGPU_COMPATIBLE;
	

	physx::PxDefaultMemoryOutputStream buf;
	if (core->m_Cooking->cookConvexMesh(网格描述, buf)) {
		physx::PxDefaultMemoryInputData id(buf.getData(), buf.getSize());

		m_Px网格 = core->m_Physics->createConvexMesh(id);
		//PxConvexMesh* Px网格 = f_Px_创建凸包碰撞网格(core->m_Physics, core->m_Cooking, mesh);
		m_Geom = new PxConvexMeshGeometry(m_Px网格, PxMeshScale(PxVec3(scale.x, scale.y, scale.z)) );
	}
	else {
		m_Px网格 = 0;
		//m_Geom = 0;
	}

}

S_Px凸壳几何体::~S_Px凸壳几何体(){
	//if (m_Geom) delete m_Geom;
	if (m_Px网格) m_Px网格->release();
}









S_Px网格几何体::S_Px网格几何体(S_Px物理引擎核心* core, const S_三角Mesh mesh, bool 动态) {
	m_物理引擎枚举 = E_物理引擎::E_物理引擎_PhysX;
	m_Type = E_物理碰撞几何类型::E_碰撞几何_网格;

	if (动态) {
		physx::PxTolerancesScale scale;
		physx::PxCookingParams params(scale);
		// disable mesh cleaning - perform mesh validation on development configurations
		params.meshPreprocessParams |= physx::PxMeshPreprocessingFlag::eDISABLE_CLEAN_MESH;
		// disable edge precompute, edges are set for each triangle, slows contact generation
		params.meshPreprocessParams |= physx::PxMeshPreprocessingFlag::eDISABLE_ACTIVE_EDGES_PRECOMPUTE;
		// lower hierarchy for internal mesh
		params.meshCookingHint = physx::PxMeshCookingHint::eCOOKING_PERFORMANCE;

		core->m_Cooking->setParams(params);
	}
	
	


	physx::PxTriangleMeshDesc 网格描述;

	网格描述.points.count = mesh.m_vNum;
	网格描述.points.stride = sizeof(vec3);
	网格描述.points.data = mesh.m_vert;

	网格描述.triangles.count = mesh.m_iNum;
	网格描述.triangles.stride = sizeof(uvec3);
	网格描述.triangles.data = mesh.m_index;
	//网格描述.flags;

	//for (uint32 i = 0; i < mesh.m_vNum; ++i) cout << mesh.m_vert[i].z << endl;
	//for (uint32 i = 0; i < mesh.m_iNum; ++i) cout << mesh.m_index[i] << endl;
	physx::PxDefaultMemoryOutputStream buf;
	physx::PxTriangleMeshCookingResult::Enum result;


	//auto& ic = phy->getPhysicsInsertionCallback();
	bool status = core->m_Cooking->cookTriangleMesh(网格描述, buf, &result);
	//physx::PxTriangleMesh* m = Cooking->createTriangleMesh(网格描述, ic, &result);
	if (status) {
		physx::PxDefaultMemoryInputData readBuffer(buf.getData(), buf.getSize());
		m_Px网格 = core->m_Physics->createTriangleMesh(readBuffer);

		m_Geom.triangleMesh = m_Px网格;
	}
}

S_Px网格几何体::~S_Px网格几何体(){
	m_Px网格->release();
}












S_Px物理材质::S_Px物理材质(S_Px物理引擎核心* core, float32 静摩擦, float32 动摩擦, float32 恢复系数) {
	m_材质 = core->m_Physics->createMaterial(静摩擦, 动摩擦, 恢复系数);
}

S_Px物理材质::~S_Px物理材质() {
	m_材质->release();
}

void S_Px物理材质::f_set物理材质数据(void* data) {
}

